//
//  UIStandardCheckBoxButton.m
//  vhStressECG
//
//  Created by jia yu on 2021/10/31.
//

#import "UIStandardCheckBoxButton.h"
#import <vhECGTools/vhECGTools.h>
#import <ZMFoundation/ZMFoundation.h>
@interface UIStandardCheckBoxButton(){
    float screenRatio;
    UIImage *selectedImage;
    UIImage *unselectImage;
}

@end

@implementation UIStandardCheckBoxButton
@synthesize changedHandler;
-(void)changed:(id)sender{
    self.selected = !self.selected;
    if (changedHandler){
        changedHandler(self.selected);
    }
}

-(UIImage *)resizeImage:(UIImage *)fromImage{
    CGSize tSize = CGSizeMake(self.frame.size.height, self.frame.size.height);
    UIGraphicsBeginImageContextWithOptions(tSize, 0, UIScreen.mainScreen.scale);
    [fromImage drawInRect:CGRectMake(0,0, tSize.width, tSize.height)];
    UIImage *res  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    res = [res stretchableImageWithLeftCapWidth:res.size.width topCapHeight:0];
    return res;
}

-(id)initWithFrame:(CGRect)frame{
    return [[UIStandardCheckBoxButton alloc] initWithFrame:frame scale:1.f];
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        screenRatio = scale;
        NSBundle *frameBundle = [NSBundle ex_bundleWithName:@"vhECGTools"];
        NSBundle *myBundle = [[NSBundle alloc] initWithPath:[frameBundle pathForResource:@"standard" ofType:@"bundle"]];
        selectedImage = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"checkbox_select" ofType:@"png"]];
        UIImage *targetImage = [self resizeImage:selectedImage];
        [self setBackgroundImage:targetImage forState:UIControlStateHighlighted];
        [self setBackgroundImage:targetImage forState:UIControlStateSelected];
        
        unselectImage = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"checkbox_unselect" ofType:@"png"]];
        targetImage = [self resizeImage:unselectImage];
        [self setBackgroundImage:targetImage forState:UIControlStateNormal];
        
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * screenRatio];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        [self setTitleColor:[vhECGTools colorFromHEXString:@"606972"] forState:UIControlStateNormal];
        [self setTitleColor:[vhECGTools colorFromHEXString:@"606972" withAlpha:0.4f] forState:UIControlStateDisabled];
        self.titleEdgeInsets = UIEdgeInsetsMake(0.f, 80.f * screenRatio, 0.f, -80.f * screenRatio);
        
        [self addTarget:self action:@selector(changed:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    UIImage *targetImage = [self resizeImage:selectedImage];
    [self setBackgroundImage:targetImage forState:UIControlStateHighlighted];
    [self setBackgroundImage:targetImage forState:UIControlStateSelected];
    targetImage = [self resizeImage:unselectImage];
    [self setBackgroundImage:targetImage forState:UIControlStateNormal];
}

@end
